package client;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

/**
 *
 * Classe permettant d'envoyer des donnees au serveur.
 */
public class ClientEnvoie extends Thread {
        
		/**
		 * Socket de connexion
		 */
		Socket socket;
		
        BufferedReader systemIn;
        /**
         * pseudo du client
         */
        String pseudo;

        /**
         * Cree une instance de client envoi.
         * @param Socket socket
         * @param String pseudo 
         */
        public ClientEnvoie(Socket socket, String pseudo) {
                this.socket = socket;
                this.pseudo = pseudo;
                this.systemIn = new BufferedReader(new InputStreamReader(System.in));

        }
       
        @Override
        public void run() {
                try {
                        String message;
                        PrintWriter out =
                                           new PrintWriter(
                                                 new BufferedWriter(
                                                       new OutputStreamWriter(socket.getOutputStream())),
                                                 true);
                        out.println(pseudo);
                       
                        while ((message = this.systemIn.readLine()) != null) {
                                out.println(message);                  
                                if(message.equals("end"))
                                        break;

                        }
                        out.close();
                        systemIn.close();
                        socket.close();
                } catch (IOException e) {}
        }
}
